<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建 Internet 或 Unix 域服务器套接字</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.stream-socket-sendto.html">« stream_socket_sendto</a></li>
      <li style="float: right;"><a href="function.stream-socket-shutdown.html">stream_socket_shutdown »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.stream.html">Stream 函数</a></li>
    <li>创建 Internet 或 Unix 域服务器套接字</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.stream-socket-server" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_server</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">stream_socket_server</span> &mdash; <span class="dc-title">创建 Internet 或 Unix 域服务器套接字</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-socket-server-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_socket_server</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$address</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$error_code</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_message</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">resource</span><span class="type"></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code>null</code></strong></span></span><br>): <span class="type"><span class="type">resource</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   在指定 <code class="parameter">address</code> 上创建 stream 或者数据包套接字（datagram socket）。
  </p>
  <p class="para">
   此函数仅创建套接字，并使用 <span class="function"><a href="function.stream-socket-accept.html" class="function">stream_socket_accept()</a></span> 开始接受连接。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-server-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">address</code></dt>

     <dd>

      <p class="para">
       传输器决定创建的套接字类型，其传输器由标准 URL 格式（<code class="literal">transport://target</code>）指定。
      </p>
      <p class="para">
       对于 Internet 域套接字（<strong><code>AF_INET</code></strong>），比如 TCP 和 UDP，
       <code class="parameter">remote_socket</code> 参数的 <code class="literal">target</code>
       部分应由主机名或者 IP 地址，随后跟着冒号和端口号组成。
       对于 Unix 域套接字， <code class="literal">target</code> 部分指向文件系统中的套接字文件。
      </p>
      <p class="para">
       环境的不同，可能会导致 Unix 域套接字无法使用。可以使用 <span class="function"><a href="function.stream-get-transports.html" class="function">stream_get_transports()</a></span>
       检索可以使用的传输器列表。查看<a href="transports.html" class="xref">所支持的套接字传输器列表</a>获取内置传输器列表。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">error_code</code></dt>

     <dd>

      <p class="para">
       如果指定了可选的 <code class="parameter">error_code</code> 和 <code class="parameter">error_message</code>
       参数，它们将被设置为指出在系统级别进行 <code class="literal">socket()</code>、<code class="literal">bind()</code>、
       <code class="literal">listen()</code> 调用时发生的真实系统级别错误。如果 <code class="parameter">error_code</code>
       返回的值为 <code class="literal">0</code> 且函数返回 <strong><code>false</code></strong>，则表明错误发生在 <code class="literal">bind()</code>
       调用之前。请注意 <code class="parameter">error_code</code> 和 <code class="parameter">error_message</code>
       始终通过引用传递。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">error_message</code></dt>

     <dd>

      <p class="para">
       参阅 <code class="parameter">error_code</code> 描述。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       位掩码字段，可以设置为套接字创建 flag 的任意组合。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        对于 UDP 套接字，你必须使用 <strong><code>STREAM_SERVER_BIND</code></strong> 作为
        <code class="parameter">flags</code> 参数。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-server-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回已创建的 stream，错误时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-socket-server-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">context</code> 现在可为 null。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-socket-server-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3788">
    <p><strong>示例 #1 使用 TCP 服务器套接字</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://0.0.0.0:8000"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_accept</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'The&nbsp;local&nbsp;time&nbsp;is&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'n/j/Y&nbsp;g:i&nbsp;a'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   下面的示例展示了如何充当时间服务器，可以响应时间查询，正如 <span class="function"><a href="function.stream-socket-client.html" class="function">stream_socket_client()</a></span> 上的示例。
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     大多数系统在低于 1024 的端口号上创建服务器套接字时需要 root 访问权限。
    </span>
   </p></blockquote>
   <div class="example" id="example-3789">
    <p><strong>示例 #2 使用 UDP 服务器套接字</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1:1113"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_SERVER_BIND</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)"</span><span style="color: #007700">);<br />}<br /><br />do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$pkt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$peer</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$peer</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stream_socket_sendto</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"D&nbsp;M&nbsp;j&nbsp;H:i:s&nbsp;Y\r\n"</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$peer</span><span style="color: #007700">);<br />}&nbsp;while&nbsp;(</span><span style="color: #0000BB">$pkt&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-socket-server-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">当指定数值型的 IPv6
地址（例如 <code class="literal">fe80::1</code>）时必须用方括号将 IP 围起来——例如，
<code class="literal">tcp://[fe80::1]:80</code>。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-socket-server-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.stream-socket-client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
   <li class="member"><span class="function"><a href="function.stream-set-blocking.html" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - 为资源流设置阻塞或者阻塞模式</span></li>
   <li class="member"><span class="function"><a href="function.stream-set-timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
   <li class="member"><span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
   <li class="member"><span class="function"><a href="function.fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a> - 从文件指针中读取一行并过滤掉 HTML 标记</span></li>
   <li class="member"><span class="function"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件（可安全用于二进制文件）</span></li>
   <li class="member"><span class="function"><a href="function.fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - 关闭一个已打开的文件指针</span></li>
   <li class="member"><span class="function"><a href="function.feof.html" class="function" rel="rdfs-seeAlso">feof()</a> - 测试文件指针是否到了文件结束的位置</span></li>
   <li class="member"><a href="ref.curl.html" class="link">Curl 扩展</a></li>
  </ul>
 </div>

</div></div></div></body></html>